(when (>= emacs-major-version 24)
  (require 'package)
  (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t))
(require 'cl)
(defvar lingang/packages '(
			   company
			   monokai-theme
			   hungry-delete
			   swiper
			   counsel
			   smartparens
			   js2-mode
			   exec-path-from-shell
			   nodejs-repl
			   popwin
			   web-mode
			   js2-refactor
			   expand-region
			   iedit
			   org-pomodoro
			   evil
			   ) "Default packages")

(defun lingang/packages-installed-p ()
  (loop for pkg in lingang/packages
	when (not (package-installed-p pkg)) do (return nil)
	finally (return t)))

(unless (lingang/packages-installed-p)
  (message "%s" "Refreshing package database...")
  (package-refresh-contents)
  (dolist (pkg lingang/packages)
    (when (not (package-installed-p pkg))
      (package-install pkg))))

;; if Emacs run under mac-os, then initialize exec-path-from-shell
(when (memq window-system '(mac ns)) (exec-path-from-shell-initialize))

;; hungry-delete mode
(require 'hungry-delete)
(global-hungry-delete-mode)

;; smartparens-mode
(smartparens-global-mode t)
;; emacs-lisp-mode smartparen-mode do not active when you input "'"
(sp-local-pair 'emacs-lisp-mode "'" nil :actions nil)

;; ivy-mode
(ivy-mode 1)
(setq ivy-use-virtual-buffers t)
(setq enable-recursive-minibuffers t)

;; js2-mode
(setq auto-mode-alist
      (append
       '(("\\.js\\'" . js2-mode)
	 ("\\.html\\'" . web-mode))
       auto-mode-alist))
;; company-mode
(global-company-mode t)

;; config for web-mode
(defun my-web-mode-indent-setup ()
  (setq web-mode-markup-indent-offset 2)
  (setq web-mode-css-indent-offset 2)
  (setq web-mode-code-indent-offset 2))

(add-hook 'web-mode-hook 'my-web-mode-indent-setup)

(defun my-toggle-web-indent ()
  (interactive)
  (if (or (eq major-mode 'js-mode) (eq major-mode 'js2-mode))
      (progn
	(setq js-indent-level (if (= js-indent-level 2) 4 2))
	(setq js2-basic-offset (if (= js2-basic-offset 2) 4 2))))

  (if (eq major-mode 'web-mode)
      (progn
	(setq web-mode-markup-indent-offset (if (= web-mode-markup-indent-offset 2) 4 2))
	(setq web-mode-css-indent-offset (if (= web-mode-css-indent-offset 2) 4 2))
	(setq web-mode-code-indent-offset (if (= web-mode-code-indent-offset 2) 4 2))))

  (if (eq major-mode 'css-mode)
      (setq css-indent-offset (if (= css-indent-offset 2) 4 2)))

  (setq indent-tabs-mode nil))

;; popwin-mode
(require 'popwin)
(popwin-mode t)

;; config for js2-refactor
(add-hook 'js2-mode-hook #'js2-refactor-mode)

;; load theme
(load-theme 'monokai t)

;; org-pomodoro
(require 'org-pomodoro)

;;;###autoload
(defun packages-autoload()
  (interactive)
  (message "----> packages is loaded!"))

;; if you use load or load-file import this, provide is needed to execute,
;; if you use require, provide is not nesseray
(provide 'init-packages)
